<?php
session_start();
require_once('inc_wtfaq.php');

//var_dump($_SESSION['wtfaq']);
if ( $_SESSION['wtfaq']['UID'] > 0 && !empty($_GET['question']) && !empty($_GET['answer']) ) {	
	//Ten eerste kijken of het een bestaande wtfaq is en een nieuwe
	if (intval($_SESSION['wtfaq']['wtfaqId'])==0) {
		$query = sprintf("INSERT INTO wtfaq_wtfaq(geocoords, location, title, user_id) VALUES (GeomFromText( 'POINT(%s %s)' ), '%s','%s', %d)",
						$_SESSION['wtfaq']['geocoords'][0],
						$_SESSION['wtfaq']['geocoords'][1],
						$_SESSION['wtfaq']['location'],
						$_SESSION['wtfaq']['title'],
						$_SESSION['wtfaq']['UID']
						);
		mysql_query($query);
		$_SESSION['wtfaq']['wtfaqId'] = mysql_insert_id();
	}
	elseif (intval($_SESSION['wtfaq']['wtfaqId']) > 0) {
	//deze wordt nu bij elke post van een vraag/antwoord geupdate == is eigenlijk niet nodig
		$query = sprintf("UPDATE wtfaq_wtfaq
							SET
						geocoords = GeomFromText( 'POINT(%s %s)' )
						,location  = '%s'
						, title = '%s'
						WHERE id = %d",
						$_SESSION['wtfaq']['geocoords'][0],
						$_SESSION['wtfaq']['geocoords'][1],
						$_SESSION['wtfaq']['location'],
						$_SESSION['wtfaq']['title'],
						$_SESSION['wtfaq']['wtfaqId']
						);
		mysql_query($query);
	}
	//vervolgens wordt de subtabel met de vraag/antwoorden bijgewerkt
	
	if (intval($_SESSION['wtfaq']['wtfaqId']) > 0 && empty($_GET['questionId'])) {
		$query = sprintf("INSERT INTO wtfaq_question(wtfaq_id, question, answer) VALUES ( %d, '%s','%s')",
						$_SESSION['wtfaq']['wtfaqId'],
						mysql_real_escape_string($_GET['question']),
						mysql_real_escape_string($_GET['answer'])
						);
		mysql_query($query);
	}
	elseif ($_SESSION['wtfaq']['wtfaqId'] > 0 && intval($_GET['questionId']) > 0) {
		$query = sprintf("UPDATE wtfaq_question SET question= '%s', answer = '%s' WHERE id = %d LIMIT 1" ,
						mysql_real_escape_string($_GET['question']),
						mysql_real_escape_string($_GET['answer']),
						intval($_GET['questionId'])
						);
		mysql_query($query);
	}
	
	$sts = 'ok';
	$questions = get_questions_of_sessionfaq($sts);
	print '{"status":"ok", "msg": "nieuw : '.$sts.' '.$query.'", "questions": '.$questions.',"wtfaqId": "'.$_SESSION['wtfaq']['wtfaqId'].'"}';

} // Als er nog geen sessie is dan ben je net ingelogd en heb je een nieuwe wtfaq aangemaakt of een bestaande gekozen
elseif ($_SESSION['wtfaq'][UID] > 0 && isset($_GET['location']) && isset($_GET['geocoords']) && isset($_GET['title'])) {
		$_SESSION['wtfaq']['location'] = $_GET['location'];
		$aPoint = json_decode(str_replace("\\","",$_GET['geocoords']), true);
		$_SESSION['wtfaq']['geocoords'] = array_values($aPoint);
		$_SESSION['wtfaq']['title'] = $_GET['title'];
		$_SESSION['wtfaq']['wtfaqId'] = (int) $_GET['selwtfaq'];
		$sts = 'ok';
		$questions = get_questions_of_sessionfaq($sts);
		print '{"status":"ok", "msg": "'.$sts.'", "questions": '.$questions.'}';
} 
else {
//in alle andere gevallen is het een foutsituatie
		print '{ "status": "error", "msg": "incomplete data or not logged in" }';
}

function get_questions_of_sessionfaq(&$sts) {
		if  ( $_SESSION['wtfaq']['wtfaqId'] > 0 ) {
			$query = "SELECT id, question, answer FROM wtfaq_question WHERE wtfaq_id='".$_SESSION['wtfaq']['wtfaqId']."'";
			$result = mysql_query($query);

			if (!$result) {
				$message  = 'Invalid query: ' . mysql_error() . "\n";
				$message .= 'Whole query: ' . $query;
				$sts = $message;
				return '[]';
			}
			
			$questions = array();
			while ($rij = mysql_fetch_assoc($result)) {
				$questions[] = $rij;
			}
			return json_encode($questions);
		}
	return "[]";
}
?>